<?php


/*
 * ajax请求返回数据统一格式//////
 */
if (!function_exists('activeSuccess')) {
    function activeSuccess($data = '', $message = '操作成功', $code = 200)
    {
        return json_encode(['status' => true, 'data' => $data, 'msg' => $message, 'code' => $code]);
    }
}


if (!function_exists('activeFail')) {
    function activeFail($data = '', $message = '操作失败', $code = 500)
    {
        return json_encode(['status' => false, 'data' => $data, 'msg' => $message, 'code' => $code]);
    }
}



if (!function_exists('curlPost')) {
    function curlPost($url, $postFields)
    {
        $postFields = json_encode($postFields);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
            )
        );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec($ch);
        if (false == $ret) {
            $result = curl_error($ch);
        } else {
            $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 " . $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close($ch);
        return $result;
    }
}


if(!function_exists('curlGet')){
    function curlGet($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}



//生成自定义前缀，位数，随机字符串
function getDiyUniqueCode($str,$len=5){
    $code =  $str.date('YmdHi').mt_rand(pow(10,$len-1),pow(10,$len)-1);
    return $code;
}



//格式化时间
if (!function_exists('formatTime')) {

    function formatTime($time)
    {
        date_default_timezone_set('PRC');
        $time = strtotime($time);
        $etime = time() - $time;
        switch ($etime){
            case $etime <= 60:
                $msg = '刚刚';
                break;
            case $etime > 60 && $etime <= 60 * 60:
                $msg = floor($etime / 60) . ' 分钟前';
                break;
            case $etime > 60 * 60 && $etime <= 24 * 60 * 60:
                $msg = date('Ymd',$time)==date('Ymd',time()) ? '今天 '.date('H:i',$time) : '昨天 '.date('H:i',$time);
                break;
            case $etime > 24 * 60 * 60 && $etime <= 2 * 24 * 60 * 60:
                $msg = date('Ymd',$time)+1==date('Ymd',time()) ? '昨天 '.date('H:i',$time) : '前天 '.date('H:i',$time);
                break;
            case $etime > 2 * 24 * 60 * 60 && $etime <= 12 * 30 * 24 * 60 * 60:
                $msg = date('Y',$time)==date('Y',time()) ? date('m-d H:i',$time) : date('Y-m-d',$time);
                break;
            default: $msg = date('Y-m-d',$time);
        }
        return $msg;
    }
}


function loginUserInfo(){
    $authInfo = auth('api')->user();
    return $authInfo;
}



function hideStar($str)
{
    if (strpos($str, '@')) {
        $email_array = explode("@", $str);
        $strlen     = mb_strlen($email_array[0], 'utf-8');
        $firstStr     = mb_substr($str, 0, 2, 'utf-8');
        $lastStr     = mb_substr($email_array[0], -1, 1, 'utf-8');
        $rs = $firstStr . str_repeat("*", $strlen - 2) . $lastStr .'@'. $email_array[1] ;
    } else {
        $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i';
        if (preg_match($pattern, $str)) {
            $rs = preg_replace($pattern, '$1****$2', $str);
        } else {
            $rs = substr($str, 0, 3) . "***" . substr($str, -1);
        }
    }
    return $rs;
}


function substr_cut($user_name){
    $strlen     = mb_strlen($user_name, 'utf-8');
    $firstStr     = mb_substr($user_name, 0, 1, 'utf-8');
    $lastStr     = mb_substr($user_name, -1, 1, 'utf-8');
    return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
}

//计算两个日期相差天数
if (!function_exists('getDateDays')) {
    function getDateDays($start,$end)
    {
        $srartDate = substr($start,0,10);
        $endDate = substr($end,0,10);
        $srartTimestamp = strtotime($start);
        $endTimestamp = strtotime($end);
        if($srartDate == $endDate){
            $days = round(( $endTimestamp - $srartTimestamp)/3600/9,2);
            return $days;
        }else{
            $dayNum = round(($endTimestamp-$srartTimestamp)/3600/24);
            $noWorkTime = 15*3600*$dayNum;
            $days = round(( $endTimestamp - $srartTimestamp-$noWorkTime)/3600/9);
            return $days;
        }
    }
}


//格式化年月日
if (!function_exists('getYMD')) {
    function getYMD($date)
    {
        return date('Y/m/d',strtotime($date));
    }
}


function getClientAddress($ip)
{
    return '未知';
    $ipInfo = file_get_contents("http://apikey.net/?ip=".$ip);
    $ipInfo = json_decode($ipInfo,true);
    if(isset($ipInfo['code']) && $ipInfo['code']==200){
        return $ipInfo['address'];
    }else{
        return '未知';
    }
}
